home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Utility Library / Old / START HERE - Box / BoxShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  7.8 KB  |  301 lines  |  [TEXT/MPCC]

  1. // BoxShell.c - QuickDraw 3d routines
  2. //
  3. // This is box, the QuickDraw 3D starter program.  Written for the
  4. // Getting started with QuickDraw 3D Develop article.  This app does not have 
  5. // graceful error handling - it's putpose is to illustrate a very basic QuickDraw 
  6. // 3D program.
  7. //
  8. // Nick Thompson - January 6th 1995
  9. // 
  10. // ©1994-95 Apple computer Inc., All Rights Reserved
  11. //
  12.  
  13. // system headers
  14. #include <Devices.h>
  15. #include <Dialogs.h>
  16. #include <DiskInit.h>
  17. #include <Events.h>
  18. #include <Fonts.h>
  19. #include <Menus.h>
  20. #include <PictUtils.h>
  21. #include <QDOffScreen.h>
  22. #include <QuickDraw.h>
  23. #include <SegLoad.h>
  24. #include <StandardFile.h>
  25. #include <TextEdit.h>
  26.  
  27. // for QuickDraw 3D
  28. #include "QD3D.h"
  29. #include "QD3DMath.h"
  30. #include "QD3DDrawContext.h"
  31. #include "QD3DShader.h"
  32. #include "QD3DTransform.h"
  33. #include "QD3DGroup.h"
  34.  
  35.  
  36. #include "BoxShell.h"
  37. #include "Box3DSupport.h"
  38.  
  39. //-------------------------------------------------------------------------------------------
  40.  
  41. struct _documentRecord {
  42.     TQ3ViewObject    fView ;                    // the view for the scene
  43.     TQ3GroupObject    fModel ;                // object in the scene being modelled
  44.     TQ3StyleObject    fInterpolation ;        // interpolation style used when rendering
  45.     TQ3StyleObject    fBackFacing ;            // whether to draw shapes that face away from the camera
  46.     TQ3StyleObject    fFillStyle ;            // whether drawn as solid filled object or decomposed to components
  47.     TQ3Matrix4x4        fRotation;                // the transform for the model
  48. };
  49.  
  50. typedef struct _documentRecord DocumentRec, *DocumentPtr, **DocumentHdl ;
  51.  
  52.  
  53. //-------------------------------------------------------------------------------------------
  54. // function prototypes
  55.  
  56. static void         InitToolbox( void ) ;
  57. static void         MainEventLoop( void ) ;
  58. static void         HandleKeyPress(EventRecord *event) ;
  59. static void         HandleOSEvent(EventRecord *event) ;
  60. void InitDocumentData( DocumentPtr theDocument ) ;
  61. TQ3Status DocumentDraw3DData( DocumentPtr theDocument ) ;
  62. void DisposeDocumentData( DocumentPtr theDocument) ;
  63.  
  64.  
  65. //-------------------------------------------------------------------------------------------
  66. //
  67.  
  68. Boolean         gQuitFlag         = false ;
  69. WindowPtr        gMainWindow        = nil ;
  70. DocumentRec        gDocument ;
  71.  
  72. //-------------------------------------------------------------------------------------------
  73. // main()
  74. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  75. // and enter the main event loop.  On exit from the main event loop, we want to call
  76. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  77.  
  78. void main(void)
  79. {
  80.     TQ3Status    myStatus;
  81.     Rect        rBounds = { 50, 50, 450, 450 } ;
  82.     Str255        title = "\pSpinning Box" ;
  83.  
  84.     InitToolbox() ;
  85.     
  86.     //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  87.     myStatus = Q3Initialize();
  88.  
  89.     if ( myStatus == kQ3Failure )
  90.         DebugStr("\pErInitialize returned failure.");            
  91.  
  92.     // set up our globals
  93.     gQuitFlag = false ;
  94.     gMainWindow = NewCWindow(nil,&rBounds,title,true,noGrowDocProc,(WindowPtr)-1,true,0) ;
  95.  
  96.     InitDocumentData( &gDocument ) ;
  97.     
  98.     MainEventLoop();
  99.     
  100.     DisposeDocumentData( &gDocument ) ;
  101.     
  102.     //    Close our connection to the QuickDraw 3D library
  103.     myStatus = Q3Exit();
  104.     if ( myStatus == kQ3Failure )
  105.         DebugStr("\pErExit returned failure.");
  106.     
  107. }
  108.  
  109. //-------------------------------------------------------------------------------------------
  110. //
  111.  
  112. void InitDocumentData( DocumentPtr theDocument ) 
  113. {
  114.     // sets up the 3d data for the scene
  115.     // Create view for QuickDraw 3D.
  116.     theDocument->fView = MyNewView( (WindowPtr)gMainWindow ) ;
  117.  
  118.     // the main display group:
  119.     theDocument->fModel = MyNewModel() ;
  120.  
  121.     // the drawing styles:
  122.     theDocument->fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleNone) ;
  123.     theDocument->fBackFacing = Q3BackfacingStyle_New(kQ3BackfacingStyleBoth ) ;
  124.     theDocument->fFillStyle = Q3FillStyle_New(kQ3FillStyleFilled ) ;
  125.  
  126.     // set the rotation matrix the identity matrix
  127.     Q3Matrix4x4_SetIdentity(&theDocument->fRotation);        
  128. }
  129.  
  130. void DisposeDocumentData( DocumentPtr theDocument)
  131. {
  132.     Q3Object_Dispose(theDocument->fView) ;                // the view for the scene
  133.     Q3Object_Dispose(theDocument->fModel) ;                // object in the scene being modelled
  134.     Q3Object_Dispose(theDocument->fInterpolation) ;        // interpolation style used when rendering
  135.     Q3Object_Dispose(theDocument->fBackFacing) ;        // whether to draw shapes that face away from the camera
  136.     Q3Object_Dispose(theDocument->fFillStyle) ;            // whether drawn as solid filled object or decomposed to components
  137.  
  138. }
  139. //-----------------------------------------------------------------------------
  140. // 
  141.  
  142. TQ3Status DocumentDraw3DData( DocumentPtr theDocument )
  143. {    
  144.     Q3View_StartRendering(theDocument->fView );
  145.     do {
  146.         Q3Style_Submit( theDocument->fInterpolation, theDocument->fView );
  147.         Q3Style_Submit( theDocument->fBackFacing, theDocument->fView );
  148.         Q3Style_Submit( theDocument->fFillStyle, theDocument->fView );
  149.         Q3MatrixTransform_Submit( &theDocument->fRotation, theDocument->fView );
  150.         Q3DisplayGroup_Submit( theDocument->fModel, theDocument->fView );
  151.     } while (Q3View_EndRendering(theDocument->fView) == kQ3ViewStatusRetraverse );
  152.     return kQ3Success ;
  153. }
  154.  
  155.  
  156. //----------------------------------------------------------------------------------
  157.  
  158. //-------------------------------------------------------------------------------------------
  159. //
  160.  
  161. short HiWrd(long aLong)
  162. {
  163.     return    (((aLong) >> 16) & 0xFFFF) ;
  164. }
  165.  
  166. //-------------------------------------------------------------------------------------------
  167. //
  168.  
  169. short LoWrd(long aLong)
  170. {
  171.     return    ((aLong) & 0xFFFF) ;
  172.  
  173. }
  174.  
  175. //-------------------------------------------------------------------------------------------
  176. //
  177.  
  178. void InitToolbox()
  179. {
  180.     Handle        menuBar = nil;
  181.  
  182.     MaxApplZone() ;
  183.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  184.     
  185.     InitGraf( &qd.thePort );
  186.     InitFonts();
  187.     InitWindows();
  188.     InitCursor();
  189.  
  190.     FlushEvents( everyEvent, 0 ) ;
  191.     // initialize application globals
  192.     
  193.     gQuitFlag = false;
  194.     
  195. }
  196.  
  197.  
  198. //-------------------------------------------------------------------------------------------
  199. //
  200. void MainEventLoop()
  201. {
  202.     EventRecord     event;
  203.     WindowPtr       window;
  204.     short           thePart;
  205.     Rect            screenRect, updateRect;
  206.     Point            aPoint = {100, 100};
  207.     
  208.  
  209.     while( !gQuitFlag )
  210.     {
  211.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  212.         {
  213.  
  214.             switch (event.what) {
  215.                 case mouseDown:
  216.                 
  217.                     thePart = FindWindow( event.where, &window );
  218.                     
  219.                     switch( thePart ) {
  220.                         case inMenuBar: 
  221.                             break;
  222.                         
  223.                         case inDrag:
  224.                     
  225.                             screenRect = (**GetGrayRgn()).rgnBBox;
  226.                             DragWindow( window, event.where, &screenRect );
  227.                             break ;
  228.                     
  229.                         case inContent:
  230.                     
  231.                             if (window != FrontWindow())
  232.                                 SelectWindow( window );
  233.                             break ;
  234.                     
  235.                         case inGoAway:
  236.                             if (TrackGoAway( window, event.where )) {
  237.                                 DisposeWindow ( window );
  238.                                 gQuitFlag = true;
  239.  
  240.                             }
  241.                             break ;
  242.                             
  243.                         default:
  244.                             break ;
  245.                     }
  246.                     break ;
  247.                             
  248.                         
  249.                 case updateEvt:
  250.                 
  251.                     window = (WindowPtr)event.message;
  252.                     updateRect = (**(window->visRgn)).rgnBBox;
  253.                     SetPort( window ) ;
  254.                     BeginUpdate( window );
  255.                     DocumentDraw3DData( &gDocument ) ;
  256.                     EndUpdate( window );
  257.                     break ;
  258.                     
  259.                 case keyDown:
  260.                 case autoKey:
  261.                     HandleKeyPress(&event);
  262.                     break;
  263.                     
  264.                 case diskEvt:
  265.                     if ( HiWrd(event.message) != noErr ) 
  266.                         (void) DIBadMount(aPoint, event.message);
  267.                     break;
  268.                     
  269.                 case osEvt:
  270.                 case activateEvt:
  271.                     break;
  272.  
  273.  
  274.             }
  275.         }
  276.         else {
  277.             // we received a null event, rotate the cube
  278.             TQ3Matrix4x4    tmp;
  279.             Rect        theRect = ((GrafPtr)gMainWindow)->portRect ;
  280.             
  281.             SetPort((GrafPtr)gMainWindow) ;
  282.             Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.1, 0.12, 0.08);
  283.             Q3Matrix4x4_Multiply(&gDocument.fRotation, &tmp, &gDocument.fRotation);
  284.  
  285.             InvalRect( &theRect ) ;
  286.         }
  287.     }
  288. }
  289.  
  290.  
  291. //-------------------------------------------------------------------------------------------
  292. //
  293. void HandleKeyPress(EventRecord *event)
  294. {}
  295.  
  296. //-------------------------------------------------------------------------------------------
  297. //
  298.  
  299.  
  300.  
  301.